#!/bin/bash

source /home/shell/util.sh



# 安装函数
function install () {
    while [ true ]; do
        printInfo "版本列表"
        curl -sL https://mirrors.tuna.tsinghua.edu.cn/mariadb/|grep -Po "mariadb-\d{2,}.\d+.\d+"|uniq|perl -pe "s/mariadb-(.*)/\1/g"
        read -p "请选择版本：" version
        if [[ -z "${version}" ]]; then
            printFail "请输入版本"
        else
            break;
        fi
    done
    
    download /opt/mariadb.tar.gz https://mirrors.tuna.tsinghua.edu.cn/mariadb/mariadb-${version}/bintar-linux-systemd-x86_64/mariadb-${version}-linux-systemd-x86_64.tar.gz
    mkdir -p /opt/mariadb/mariadb-server-data
    mkdir -p /var/log/mysql/
    tar -xf /opt/mariadb.tar.gz -C /opt/mariadb
    rm -rf /opt/mariadb.tar.gz
    mv /opt/mariadb/mariadb-*-linux-systemd-x86_64 /opt/mariadb/mariadb-server
    /opt/mariadb/mariadb-server/scripts/mariadb-install-db --basedir='/opt/mariadb/mariadb-server/' --datadir='/opt/mariadb/mariadb-server-data/'
    printInfo "安装完成！"
}

# 配置函数
function config(){
    useradd mysql
    chown -R mysql:mysql /opt/mariadb/mariadb-server-data/
    chown -R mysql:mysql /var/log/mysql/
    cp -r /home/shell/support/my.cnf /opt/mariadb/mariadb-server/my.cnf
    firewall-addport 3306
tee /etc/profile.d/mariadb.sh<<EOF
export PATH="/opt/mariadb/mariadb-server/bin:\$PATH"
EOF
    source /etc/profile.d/mariadb.sh
}

function run () {
    cd /opt/mariadb/mariadb-server/mysql-test/
    perl ./mysql-test-run.pl
bash /home/shell/serviced-install.sh<<EOF



mariadbd
/opt/mariadb/mariadb-server/bin/mysqld_safe --defaults-file='/opt/mariadb/mariadb-server/my.cnf' --basedir='/opt/mariadb/mariadb-server/' --datadir='/opt/mariadb/mariadb-server-data/'



mysql
EOF
}

# 运行安装和配置
install && config && run